#include "mwworker.h"
#include "mainwindow.h"
#include "msgproto.h"
#include <QTcpSocket>

extern "C" int Match2Fp(unsigned char* Src,unsigned char* Dst);

mwworker::mwworker(QWidget *parent)
{
    ui = (MainWindow*)parent;
    isrun = false;
}

void mwworker::msgq_push(int msg)
{
    msgq.push(msg);
}

bool mwworker::msgq_get(int &msg)
{
    if (msgq.empty())
        return false;

    msg = msgq.front();
    msgq.pop();

    return true;
}

void mwworker::ShowStatus(const char *s)
{
   string ss = s;

   emit ShowStatus(ss);
}

void mwworker::run()
{
    QTcpSocket *client;
    msgproto pkt;
    char *rxbuf;
    int size;
    upchar_req_t chardat, *upcd;
    QByteArray txbuf;

    isrun = true;

    server = new QTcpServer;
    if (!server->listen(QHostAddress::Any, 19192))
    {
        qDebug("err");
        return;
    }

WAIT:
    ui->ShowTip("等待设备连接");
    server->waitForNewConnection(-1);
    client = server->nextPendingConnection();

    rxbuf = new char[2048];

    ui->ShowTip("设备已连接");
    upcd = (upchar_req_t*)rxbuf;

    memset(chardat.data, 0, 256);

    while (isrun)
    {
        int uimsg = 0;

        if (client->state() == QTcpSocket::UnconnectedState)
        {
            ui->ShowTip("设备已断开");
            client->close();
            goto WAIT;
        }

        if (client->waitForReadyRead(500))
        {
            int cmd = 0;

            size = client->read(rxbuf, 1024);
            if (size > 0)
                emit ShowStatus("收到数据");

            if (pkt.in(rxbuf, size, cmd))
            {
                bool ret = false;

                switch (cmd)
                {
                case MP_CMD_UPCHAR:
                    ui->ShowTip("收到特征码");

                    //if (Match2Fp(upcd->data, chardat.data) > 50)
                      //  ui->ShowTip("成功");

                    chardat = *upcd;
                    break;
                case MP_CMD_DOOR:
                {
                    ui->ShowTip("收到门磁应答");
                }break;
                case MP_CMD_LTCTL:
                {
                    ui->ShowTip("收到灯应答");
                }break;
                case MP_CMD_SWV:
                {
                    ui->ShowTip("收到开关量应答");
                }break;
                case MP_CMD_RLCTL:
                {
                    ui->ShowTip("收到继电器应答");
                }break;
                }

                if (ret)
                {
                    client->write(txbuf, size);
                    client->flush();
                }
            }
        }

        msgq_get(uimsg);
        txbuf.clear();

        switch (uimsg)
        {
        case 1:
        {
            txbuf = pkt.makeDoorCtrl(1);
        }break;
        case 2:
        {
            txbuf = pkt.makeDoorCtrl(0);
        }break;
        case 3:
        {
            txbuf = pkt.makeLightCtrl(1);
        }break;
        case 4:
        {
            txbuf = pkt.makeLightCtrl(0);
        }break;
        case 5:
        {
            txbuf = pkt.makeSwitchVal(1);
        }break;
        case 6:
        {
            txbuf = pkt.makeRelaySpare(1, 1);
        }break;
        }

        if (!txbuf.isEmpty())
        {
            client->write(txbuf);
            client->flush();
            qDebug("write client");
        }
    }
}
